/**
 *
 */
package com.bsoft.gol.hcb.dao.evaluation;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import org.atag.dao.util.CustomAliasToEntityMapResultTransformer;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import pcn.sign.BaseEvaluationItemrecord;
import pcn.vo.sign.BaseEvaluationItemRecordVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @since 2017-05-03 04:17:52
 */
@SsdevDao(serviceId = "baseEvaluationItemrecordDAOService")
public abstract class BaseEvaluationItemrecordDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<BaseEvaluationItemrecord> {

	public BaseEvaluationItemrecordDAO() {
		this.setKeyField("id");
	}
	@RpcService
	@DAOMethod(sql = "select businessId from BaseEvaluationRecord  where verifyState='1' and verifyTime>:beginDate and verifyTime<:endDate and businessType in('3','18')")
	public abstract List<String> queryConsultIdListByDate(@DAOParam("beginDate") Date beginDate, @DAOParam("endDate") Date endDate);

	@RpcService
	@DAOMethod(sql = "select new map(avg(level) as score) from BaseEvaluationItemrecord where recordId in" +
            "(:recordIdList)")
	public abstract Map<String, Object> getAvgScoreByRecordIdList(@DAOParam("recordIdList") List<Integer> recordIdList);

	@RpcService
	@DAOMethod(sql = "select new map(avg(a.level) as score) from BaseEvaluationItemrecord a,BaseEvaluationRecord b where a.recordId=b.id and a.recordId in:recordIdList and b.verifyState='1'")
	public abstract Map<String, Object> getVerifyAvgScoreByRecordIdList(@DAOParam("recordIdList") List<Integer> recordIdList);

	public List<BaseEvaluationItemRecordVo> getBaseEvaluationItemrecordByRecordId(final int recordId) {
		HibernateStatelessResultAction<List<BaseEvaluationItemRecordVo>> action =
                new AbstractHibernateStatelessResultAction<List<BaseEvaluationItemRecordVo>>() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				StringBuffer hsql = new StringBuffer();
				hsql.append("select i.content content,t.level as levels,de.add_evaluation_flag from " +
						"base_evaluation_itemrecord t left join base_evaluation_item i on t.item_id=i.id ");
				hsql.append("left join base_evaluation_define de on i.define_id=de.id where t.record_id=:recordId");
				Query query = ss.createSQLQuery(hsql.toString());
				query.setParameter("recordId", recordId);
				query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
				List listi = query.list();
				if (listi.size() == 0) {
					setResult(null);
				} else {

					List<BaseEvaluationItemRecordVo> list = new ArrayList<>();
					for (Object o : listi) {
						list.add(ConversionUtils.convert(o, BaseEvaluationItemRecordVo.class));
					}

					setResult(list);
				}
			}
		};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}

	public Double getLevelAvg(final int recordId) {
		HibernateStatelessResultAction<Double> action = new AbstractHibernateStatelessResultAction<Double>() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				StringBuffer hsql = new StringBuffer();
				hsql.append("select COALESCE(ROUND(AVG(level),2),0) as avg from base_evaluation_itemrecord where " +
						"record_id=:recordId");
				Query query = ss.createSQLQuery(hsql.toString());
				query.setParameter("recordId", recordId);
				List list = query.list();
				if (list.size() == 1) {
					Object o = list.get(0);
					BigDecimal avg = (BigDecimal) o;
					setResult(avg.doubleValue());
				}
			}
		};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}
}